//
//  NewFeatureViewController.swift
//  Three-SinaWeibo
//
//  Created by 李旭阳 on 15/11/17.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let imageCount = 4

class NewFeatureViewController: UICollectionViewController {
    
    //MARK: 构造方法
    init() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        //弹簧效果
        collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //注册一个cell
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    //MARK: 数据源方法 有多少个Cell
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    //MARK: cell 创建的方法
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)  as! NewFeatureCell
        cell.index = indexPath.item
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let path = collectionView.indexPathsForVisibleItems().last! as NSIndexPath
        let newCell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
        if path.item == imageCount - 1 {
            newCell.showStartBtn()
        }
    }

}

class NewFeatureCell: UICollectionViewCell {
    
    //MARK: 显示图片的index
    var index: Int = 0 {
        didSet{
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: 按钮点击事件
    @objc private func start(){
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
    }
    
    //MARK: 动画
    private func showStartBtn(){
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("OK")
        }
    }
    
    //MARK: 设置UI控件
    private func setupUI(){
        contentView.addSubview(iconImage)
        contentView.addSubview(startButton)
        
        iconImage.snp_makeConstraints { (make) -> Void in
            //edges 等高等宽
            make.edges.equalTo(contentView.snp_edges)
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)
        }
        
        //按钮点击事件
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
        startButton.hidden = true
    }
    //MARK: 懒加载
    private lazy var iconImage: UIImageView = UIImageView ()
    
    //开始按钮
    private lazy var startButton: UIButton = UIButton(title: "开始体验", backImage: "new_feature_finish_button", color: nil)
}